﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ajayumi.develop.win32.Transport.AppUpgrade.Provider
{
    /// <summary>
    /// AppUpdateProvider.cs 应用程序更新的抽象方法类
    /// Author   : 黄乙冬
    /// Date     : 2012-02-24
    /// </summary>
    internal abstract class AppPackageProvider
    {
        #region Properties
        protected string UpdateFileDir { get; private set; }

        /// <summary>
        /// 备份文件所存放的文件夹
        /// </summary>
        protected string BackupDir
        {
            get { return Path.Combine(ArgumentSerialization.AppDirectory, Utility.BACKUP_FILE_NAME); }
        }

        #endregion

        public AppPackageProvider(string updateFileDir)
        {
            this.UpdateFileDir = updateFileDir;
        }

        /// <summary>
        /// 执行更新
        /// </summary>
        public abstract bool Update();

        public abstract bool Restore();

        protected abstract void CopyTo(IEnumerable<string> files, AppFileAction action);

        //private void CopyTo(IEnumerable<string> files, AppFileAction action)
        //{
        //    int index = 0;
        //    int length = files.Count();

        //    foreach (string item in files)
        //    {
        //        string path = string.Empty;
        //        switch (action)
        //        {
        //            case AppFileAction.RestoreFile:
        //                path = item.Replace(this.BackupDir, this.AppDirectory);
        //                break;
        //            case AppFileAction.BackupFile:
        //                path = item.Replace(this.AppDirectory, this.BackupDir);
        //                break;
        //            case AppFileAction.UpdateFile:
        //                path = item.Replace(this.UpgradeDir, this.AppDirectory);
        //                break;
        //        }

        //        FileHelper.CreateDir(path);
        //        File.Copy(item, path, true);

        //        int progressPercentage = Convert.ToInt32(Convert.ToSingle(++index) / Convert.ToSingle(length) * 100f);
        //        this.OnUpgradeProgressChanged(new UpgradeProgressChangedEventArgs(action, progressPercentage, this));
        //    }
        //}
    }
}
